# date:20210209
from typing import List
import collections

# 自己写的双指针循环，时间复杂度O(n)
# 时间：5%，内存：20%
class Solution:
    def minSubArrayLen(self, target: int, nums: List[int]) -> int:
        left, right = 0, 0
        result = 1000000
        while right <= len(nums):
            while left < right:
                if sum(nums[left: right]) >= target:
                    result = min(result, right-left)
                    left += 1
                else:
                    break
            right += 1
        return result


# 题解，双循环
# 时间：80%， 内存：30%
class Solution:
    def minSubArrayLen(self, s: int, nums: List[int]) -> int:
        i, j, l = 0, 0, len(nums)
        tem, res = 0, l + 1
        while j < l:
            tem += nums[j]
            while tem >= s:
                res = min(j - i + 1, res)
                tem -= nums[i]
                i += 1
            j += 1

        return 0 if res == l + 1 else res


model = Solution()

target = 7
nums = [2,3,1,2,4,3]
print(model.minSubArrayLen(target, nums))